package ke06.aufgabe3;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class XMLStreamReaderStAX {

	public static void main(String[] args) throws FileNotFoundException, XMLStreamException {
		XMLInputFactory factory = 
				XMLInputFactory.newInstance();
		XMLStreamReader parser = 
				factory.createXMLStreamReader(new FileInputStream("partyGen.xml"));
		
		StringBuilder spacer = new StringBuilder();
		
		int elementCounter = 0;
		int attributeCounter = 0;
		
		while(parser.hasNext()){
			
			switch(parser.getEventType())
			{
			case XMLStreamConstants.START_DOCUMENT:
				System.out.println("START_DOCUMENT");
				break;
			
			case XMLStreamConstants.START_ELEMENT:
				elementCounter++;
				spacer.append("  ");
				System.out.println(spacer+"ELEMENT: "+ parser.getLocalName());
				
				attributeCounter += parser.getAttributeCount();
				break;
			
			default:
				break;
			}
			parser.next();
		}

		System.out.println("Anzahl der Elemente: "+elementCounter);
		System.out.println("Anzahl der Attribute: "+attributeCounter);
	}

}
